package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodCilent;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-09
 */
@RestController
@RequestMapping("/eduservice/edu-video")
//@CrossOrigin
public class EduVideoController {

    //注入vodCilent
    @Autowired
    private VodCilent vodCilent;

    @Autowired
    private EduVideoService videoService;

    //增加
    @PostMapping("/addVideo")
    public R  addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }
    //删除  TODO 删除小结时将视频也删除  此时数据库是有数据的 可以通过小结id 查询出视频id
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id){

        EduVideo video = videoService.getById(id);

        String videoSourceId = video.getVideoSourceId();// 有视频才删除

        if(!StringUtils.isEmpty(videoSourceId)) {
            //根据视频id 远程调用实现视频删除
            R result = vodCilent.removeAlyVideo(videoSourceId);
            if(result.getCode() ==20001){
                throw new GuliException(20001,"删除视频失败，熔断器降级。。。");
            }
        }

        videoService.removeById(id);

        return  R.ok();
    }

    //修改 todo

}

